package com.tianlh.service.enums;

import com.google.common.base.Preconditions;

/**
 * @author 黑子
 * @Date 2018/2/2 15:51
 */
public enum OrderStatus {
    LOCK_FAILED("锁位失败"),
    LOCK_SUCCESS("已锁位"),
    CANCEL_LOCK("已取消锁位"),
    // 已经提交了出票请求还没得到返回
    COMMIT_SUCCESS("已确认提交"),
    BOOKING_ORDER("出票中"),
    BOOK_SUCCESS("出票成功"),
    BOOK_FAILED("出票失败"),
    BOOK_TIMEOUT("出票超时"),
    REFUND_SUCCESS("已退票"),
    CLOSE_ORDER("订单关闭");

    // 商家返回的状态信息
    public String desc;

    OrderStatus(String desc) {
        this.desc = desc;
    }

    public static boolean contain(String name) {
        Preconditions.checkNotNull(name);
        OrderStatus[] var = OrderStatus.values();
        for (OrderStatus var1 : var) {
            if (var1.desc.equals(name)) {
                return true;
            }
        }
        return false;
    }

    public static OrderStatus of(String name) {
        Preconditions.checkNotNull(name);
        OrderStatus[] var = OrderStatus.values();
        for (OrderStatus var1 : var) {
            // 该商家desc不重复，所以可以这样获取enum
            if (var1.desc.equals(name)) {
                return var1;
            }
        }
        return null;
    }

    public static long getStatus(OrderStatus status) {
        Preconditions.checkNotNull(status, "status is null");
        switch (status) {
            case LOCK_FAILED:
                return OrderStatusEnum.CLOSED_ORDER.getValue();
            case LOCK_SUCCESS:
                return OrderStatusEnum.INIT_ORDER.getValue();
            case CANCEL_LOCK:
                return OrderStatusEnum.CANCEL_ORDER.getValue();
            case COMMIT_SUCCESS:
            case BOOKING_ORDER:
                return OrderStatusEnum.INIT_ORDER.getValue();
            case BOOK_SUCCESS:
                return OrderStatusEnum.BOOK_SUCCESS.getValue();
            case BOOK_FAILED:
            case CLOSE_ORDER:
                return OrderStatusEnum.CLOSED_ORDER.getValue();
            case BOOK_TIMEOUT:
                return OrderStatusEnum.CANCEL_ORDER.getValue();
            case REFUND_SUCCESS:
                return OrderStatusEnum.REFUND_ORDER.getValue();
            default:
                Preconditions.checkState(false, "Illegal status %s", status);
                return 0L;
        }
    }
}